//
//  UIBarButtonItem+Extension.swift
//  weibo
//
//  Created by Apple on 16/3/8.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  文本颜色
let fontTitleColor: UIColor = UIColor(white: 80/255, alpha: 1)
//  文字大小
let fonttitleSize: CGFloat = 14

extension UIBarButtonItem {

    // MARK:    提供便利构造函数
    convenience init(imgName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        let button = UIButton()
        //  点击事件
        button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        if let img = imgName {
            //  设置不同状态的图片
            button.setImage(UIImage(named: img), forState: .Normal)
            button.setImage(UIImage(named: "\(img)_highlighted"), forState: .Highlighted)
        }
        if let t = title {
            //  设置文本
            button.setTitle(t, forState: .Normal)
            //  设置文本颜色和高亮颜色
            button.setTitleColor(fontTitleColor, forState: .Normal)
            
            button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            //  设置字体大小
            button.titleLabel?.font = UIFont.systemFontOfSize(fonttitleSize)
        }
        //  根据图片的大小，设置按钮的大小
        button.sizeToFit()
        
        customView = button
        
    }
}
